package com.sx.pipeline.ui.needs.construction.detail

import android.view.View
import androidx.recyclerview.widget.GridLayoutManager
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.lxj.xpopup.XPopup
import com.sx.mvvmlibs.base.EasyAdapter
import com.sx.pipeline.BR
import com.sx.pipeline.R
import com.sx.pipeline.adapter.ReleaseImageAdapter
import com.sx.pipeline.app.base.CYDataBindingBaseActivity
import com.sx.pipeline.app.config.IntentKey
import com.sx.pipeline.app.config.ReleaseNeedsType
import com.sx.pipeline.arouth.RouteManager
import com.sx.pipeline.arouth.RoutePath
import com.sx.pipeline.custom.dialog.CustomCommonPicket
import com.sx.pipeline.custom.dialog.DialogPasswordInput
import com.sx.pipeline.databinding.ActConstructionNeedsDetailBinding
import com.sx.pipeline.ui.needs.construction.ConstructionNeedsVM


/**
 * @Author: YY
 * @Date: 2022/4/5
 * @Description: 施工单位 需求详情
 */
@Route(path = RoutePath.Mine.CONSTRUCTION_NEEDS_DETAIL)
class ConstructionNeedsDetailActivity :
    CYDataBindingBaseActivity<ActConstructionNeedsDetailBinding, ConstructionNeedsVM>(R.layout.act_construction_needs_detail),
    View.OnClickListener {
    @JvmField
    @Autowired(name = IntentKey.ID)
    var needsId: Int? = 0

    private var mImageAdapter: ReleaseImageAdapter? = null//附件图片适配器

    override fun initData() {
        super.initData()
        mBinding.setVariable(BR.onClick, this)
        mBinding.titleBar.leftView.setOnClickListener {
            finish()
        }
        //请求需求详情
        needsId?.let { mViewModel.getNeedOrderDetail(it) }
    }

    override fun initViewObservable() {
        super.initViewObservable()
        observe(mViewModel.needOrderDetailEvent) {
            when (it?.status) {
                //待审核
                ReleaseNeedsType.PENDING_REVIEW.code -> {
                    mBinding.tvStatus.text=getString(R.string.txt_pending_review)

                    //显示取消需求
                    mBinding.tvAction.text=getString(R.string.txt_cancel_release)
                    mBinding.tvAction.visibility=View.VISIBLE

                    mBinding.sbRejectReason.visibility=View.GONE
                    mBinding.sbRejectTime.visibility=View.GONE
                }
                //待报价
                ReleaseNeedsType.TO_BE_QUOTED.code -> {
                    mBinding.tvStatus.text=getString(R.string.txt_be_quoted)

                    //通过时间
                    mBinding.sbPassTime.leftText=getString(R.string.txt_passing_time)
                    mBinding.sbPassTime.rightText=it?.auditTime
                    mBinding.sbPassTime.visibility=View.VISIBLE

                    //显示取消需求
                    mBinding.tvAction.text=getString(R.string.txt_cancel_require)
                    mBinding.tvAction.visibility=View.VISIBLE

                    mBinding.sbRejectReason.visibility=View.GONE
                    mBinding.sbRejectTime.visibility=View.GONE
                }
                //已报价
                ReleaseNeedsType.QUOTED.code -> {
                    mBinding.tvStatus.text=getString(R.string.txt_quoted)

                    //通过时间
                    mBinding.sbPassTime.leftText=getString(R.string.txt_passing_time)
                    mBinding.sbPassTime.rightText=it?.auditTime
                    mBinding.sbPassTime.visibility=View.VISIBLE

                    //显示查看报价
                    mBinding.tvAction.text=getString(R.string.txt_view_quote)
                    mBinding.tvAction.visibility=View.VISIBLE

                    mBinding.sbRejectReason.visibility=View.GONE
                    mBinding.sbRejectTime.visibility=View.GONE
                }
                //已取消
                ReleaseNeedsType.CANCELLED.code -> {
                    mBinding.tvStatus.text=getString(R.string.txt_cancelled)

                    //取消时间
                    mBinding.sbPassTime.leftText=getString(R.string.txt_cancelled_time)
                    mBinding.sbPassTime.rightText=it?.cancelTime
                    mBinding.sbPassTime.visibility=View.VISIBLE
                    mBinding.tvAction.visibility=View.GONE
                    mBinding.sbRejectReason.visibility=View.GONE
                    mBinding.sbRejectTime.visibility=View.GONE
                }
                //已过期
                ReleaseNeedsType.EXPIRED.code -> {
                    mBinding.tvStatus.text=getString(R.string.txt_expired)
                    mBinding.tvAction.visibility=View.GONE
                    mBinding.sbRejectReason.visibility=View.GONE
                    mBinding.sbRejectTime.visibility=View.GONE
                }
                //已驳回
                ReleaseNeedsType.REJECTED.code -> {
                    mBinding.tvStatus.text=getString(R.string.txt_rejected)
                    mBinding.sbRejectReason.rightText=it?.rejectDesc
                    mBinding.sbRejectTime.rightText=it?.auditTime

                    mBinding.sbRejectReason.visibility=View.VISIBLE
                    mBinding.sbRejectTime.visibility=View.VISIBLE
                    mBinding.tvAction.visibility=View.GONE
                }
            }
            //发起单位名称
            mBinding.sbUnitName.rightText=it?.unitName
            //标题
            mBinding.sbTitle.rightText=it?.title
            //需求描述
            mBinding.sbRequirementDetails.rightText=it?.description
            //项目名称
            mBinding.sbProjectName.rightText=it?.name
            //需求类型
            mBinding.sbRequirementType.rightText=it?.needType
            //需求量
            mBinding.sbDemand.rightText=it?.omsNeedServe?.quantity
            //计量单位
            mBinding.sbMeasurementUnit.rightText=it?.omsNeedServe?.unit
            //需求服务地点
            mBinding.sbAddress.rightText=it?.addressDetail
            //任务性质
            mBinding.sbNatureTask.rightText=it?.omsNeedServe?.nature
            //开始时间
            mBinding.sbStartTime.rightText=it?.omsNeedServe?.startDate
            //结束时间
            mBinding.sbEndTime.rightText=it?.omsNeedServe?.endDate
            //结算方式
            mBinding.sbSettlementMethod.rightText=it?.omsNeedServe?.paymentMethod
            //联系人
            mBinding.sbContact.rightText=it?.contact
            //联系电话
            mBinding.sbContactNumber.rightText=it?.contactPhone
            //审核人
            mBinding.sbReviewer.rightText=it?.auditBy
            //附件
            it?.omsNeedServe?.appendixUrl?.split(",")?.let { it1 -> initDocutmentImg(it1) }
            //需求创建人
            mBinding.sbCreateby.rightText=it?.createBy
            //创建时间
            mBinding.sbCreateTime.rightText=it?.createTime

        }

        observe(mViewModel.successNeedEvent){
            if (it==true){
                //请求需求详情
                needsId?.let { mViewModel.getNeedOrderDetail(it) }
            }
        }
    }

    //附件
    fun initDocutmentImg(data: List<String>) {
        mImageAdapter = ReleaseImageAdapter(this, data)
        mBinding.rvAttachment.layoutManager =
            GridLayoutManager(this, 3)
        mBinding.rvAttachment.adapter = mImageAdapter
        mImageAdapter?.selectMode = EasyAdapter.SelectMode.SINGLE_SELECT
    }


    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.tv_action->{
                when (mViewModel.needOrderDetailEvent.value?.status) {
                    //待审核
                    ReleaseNeedsType.PENDING_REVIEW.code -> {
                        //取消发布
                        XPopup.Builder(this)
                            .isDestroyOnDismiss(true)
                            .asConfirm(
                                getString(R.string.hint), getString(R.string.txt_cancel_post_needs_tips),
                                getString(R.string.cancel), getString(R.string.confirm),
                                {
                                    needsId?.let { mViewModel.cancelNeedOrder(it) }
                                }, null, false
                            ).show()
                    }
                    //待报价
                    ReleaseNeedsType.TO_BE_QUOTED.code -> {
                       //取消需求
                        XPopup.Builder(this)
                            .isDestroyOnDismiss(true)
                            .asConfirm(
                                getString(R.string.hint), getString(R.string.txt_cancel_needs_tips),
                                getString(R.string.cancel), getString(R.string.confirm),
                                {
                                    needsId?.let { mViewModel.cancelNeedOrder(it) }
                                }, null, false
                            ).show()
                    }
                    //已报价
                    ReleaseNeedsType.QUOTED.code -> {
                       //查看报价
                        needsId?.let { RouteManager.Mine.intentViewQuoted(it) }
                    }
                }
            }
            R.id.sb_address -> {
                ARouter.getInstance().build(RoutePath.Main.SHOW_LOCATION)
                    .withString("lat", mViewModel.needOrderDetailEvent.value?.latitude)
                    .withString("lon",mViewModel.needOrderDetailEvent.value?.longitude)
                    .withString("address",mViewModel.needOrderDetailEvent.value?.addressDetail)
                    .navigation()
            }
        }
    }

}